package com.example.database.controller;

import com.example.database.entity.*;
import com.example.database.enums.Status;
import com.example.database.service.StudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/student")
public class StudentController
{
    @Resource
    private StudentService studentService;

    @PostMapping(value = "/login")
    public boolean login(@RequestParam(value = "student_id") Integer student_id,
                         @RequestParam(value = "password") String password, HttpSession session)
    {
        return studentService.login(student_id, password, session);
    }

    @GetMapping(value = "/info")
    public Student getStudentInfo(HttpSession session)
    {
        return (Student) session.getAttribute("student");
    }

    @PutMapping(value = "/update")
    public boolean updateStudentInfo(@RequestBody Student student)
    {
        return studentService.updateStudent(student);
    }

    @PostMapping(value = "/admission_form/apply")
    public boolean applyAdmissionFrom(@RequestBody AdmissionForm admissionForm)
    {
        return studentService.addAdmissionForm(admissionForm);
    }

    @GetMapping(value = "/admission_form/waiting")
    public List<AdmissionForm> getWaitingAdmissionForm(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        List<AdmissionForm> admissionFormsBegin = studentService.findAdmissionFormByStudentId(student.getStudent_id(), Status.BEGIN);
        List<AdmissionForm> admissionFormsInView = studentService.findAdmissionFormByStudentId(student.getStudent_id(), Status.INVIEW);
        admissionFormsBegin.addAll(admissionFormsInView);
        return admissionFormsBegin;
    }

    @GetMapping(value = "/admission_form/pass")
    public List<AdmissionForm> getPassAdmissionForm(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        return studentService.findAdmissionFormByStudentId(student.getStudent_id(), Status.PASS);
    }

    @GetMapping(value = "/admission_form/reject")
    public List<AdmissionForm> getRejectAdmissionForm(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        return studentService.findAdmissionFormByStudentId(student.getStudent_id(), Status.FAIL);
    }

    @PostMapping(value = "/leaving_form/apply")
    public boolean applyLeavingFrom(@RequestBody LeavingForm leavingForm)
    {
        return studentService.addLeavingForm(leavingForm);
    }

    @GetMapping(value = "/leaving_form/waiting")
    public List<LeavingForm> getWaitingLeavingForm(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        List<LeavingForm> leavingFormsBegin = studentService.findLeavingFormByStudentId(student.getStudent_id(), Status.BEGIN);
        List<LeavingForm> leavingFormsInView = studentService.findLeavingFormByStudentId(student.getStudent_id(), Status.INVIEW);
        leavingFormsBegin.addAll(leavingFormsInView);
        return leavingFormsBegin;
    }

    @GetMapping(value = "/leaving_form/pass")
    public List<LeavingForm> getPassLeavingForm(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        return studentService.findLeavingFormByStudentId(student.getStudent_id(), Status.PASS);
    }

    @GetMapping(value = "/leaving_form/reject")
    public List<LeavingForm> getRejectLeavingForm(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        return studentService.findLeavingFormByStudentId(student.getStudent_id(), Status.FAIL);
    }
    // TODO
    @GetMapping(value = "/total_time_out")
    public String getTotalLeaveTimeOneYear(HttpSession session)
    {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return null;
        return studentService.OutOfSchoolTimeOneYearByStudentId(student.getStudent_id());
    }

    @PostMapping(value = "/health_form/apply")
    public boolean applyHealthForm(@RequestBody HealthForm healthForm)
    {
        return studentService.addHealthForm(healthForm);
    }

    @GetMapping(value = "/campus/students")
    public List<Student> findStudentByCampus(@RequestParam(value = "campusId") Integer campusId)
    {
        return studentService.findStudentByCampus(campusId);
    }
    // TODO
    @GetMapping(value = "/campus/in_out_log")
    public List<InOutLog> findLogByCampus(@RequestParam(value = "campusId") Integer campusId)
    {
        return studentService.findLogByCampus(campusId);
    }

    @GetMapping(value = "/class_instructor")
    public ClassInstructor findClassInstructorByStudentId(@RequestParam(value = "studentId") Integer studentId)
    {
        return studentService.findClassInstructorByStudentId(studentId);
    }

    // TODO
    @GetMapping(value = "/health_info")
    public List<HealthForm> findNDaysHealthInfo(@RequestParam(value = "studentId") Integer studentId,
                                                @RequestParam(value = "nDays") Integer nDays)
    {
        return studentService.findNDaysHealthInfo(studentId, nDays);
    }

    @GetMapping(value = "/admission_form/student_id")
    public List<AdmissionForm> findAdmissionFormByStudentId(@RequestParam(value = "studentId") Integer studentId,
                                                            @RequestParam(value = "state") Status state)
    {
        return studentService.findAdmissionFormByStudentId(studentId, state);
    }

    @GetMapping(value = "/leaving_form/student_id")
    public List<LeavingForm> findLeavingFormByStudentId(@RequestParam(value = "studentId") Integer studentId,
                                                        @RequestParam(value = "state") Status state)
    {
        return studentService.findLeavingFormByStudentId(studentId, state);
    }
    // TODO
    @GetMapping(value = "/OutOfSchoolTimeOneYear")
    public String OutOfSchoolTimeOneYearByStudentId(@RequestParam(value = "studentId") Integer studentId)
    {
        return studentService.OutOfSchoolTimeOneYearByStudentId(studentId);
    }

    // 已提交出校申请但未离校的学生数量、个人信息；
    @GetMapping(value = "/regOutButInSchoolNum")
    public Integer regOutButInSchoolNum()
    {
        return studentService.regOutButInSchoolNum();
    }

    // 已提交出校申请但未离校的个人信息；
    @GetMapping(value = "/regOutButInSchoolStudentInfo")
    public List<Student> regOutButInSchoolStudentInfo()
    {
        return studentService.regOutButInSchoolStudentInfo();
    }

    // 已出校但尚未返回校园（即离校状态）的学生数量；
    @GetMapping(value = "/outSchoolStudentNum")
    public Integer outSchoolStudentNum()
    {
        return studentService.outSchoolStudentNum();
    }
    // 已出校但尚未返回校园（即离校状态）个人信息(包含了)各自的离校时间；
    @GetMapping(value = "/outSchoolStudentInfo")
    public List<Student> outSchoolStudentInfo()
    {
        return studentService.outSchoolStudentInfo();
    }
}
